<?php

namespace addons\zzcash\controller\api;

use addons\zzcash\model\InM;
use addons\zzcash\model\OutM;
use app\common\controller\Api;
use app\common\model\User;
use Symfony\Component\HttpFoundation\Response;
use think\Validate;
use Yansongda\Pay\Pay;

class In extends Api
{
    protected $noNeedLogin = 'cb';
    protected $noNeedRight = 'cb,index';

    public function index()
    {
        $validate = new Validate();
        if (!$validate->check(input(), [
            'money' => 'require|integer'
        ])) {
            $this->error($validate->getError());
        }
        $money  = input('money/d');
        $user   = $this->auth->getUser();
        $in     = InM::create([
            'user_id'  => $user['id'],
            'payed_is' => OutM::PAYED_IS_NO,
        ]);
        $params = Pay::alipay([])->mini([]);
        $this->success('', $params);
    }

    public function cb(): Response
    {
        $pay   = Pay::alipay([]);
        $res   = $pay->verify();
        $id    = $res['id'];
        $money = $res['money'];
        $in    = InM::find($id);
        $in->save([
            'payed_is' => OutM::PAYED_IS_YES,
        ]);
        User::money($money, $in['user_id'], '充值');
        return $pay->success()->send();
    }
}
